package com.zhida.jiyangcenter

import android.app.ActivityManager
import android.content.Context
import chooongg.base.BaseApplication
import chooongg.base.Utils
import chooongg.base.loadState.LoadUtils
import chooongg.base.loadState.callback.EmptyCallback
import chooongg.base.loadState.callback.ErrorCallback
import chooongg.base.loadState.callback.LoadingCallback
import chooongg.base.loadState.callback.NetworkCallback
import com.mob.MobSDK
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator
import com.scwang.smartrefresh.layout.api.RefreshHeader
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.header.BezierRadarHeader
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.scwang.smartrefresh.layout.header.FalsifyHeader
import com.scwang.smartrefresh.layout.header.TwoLevelHeader


class APP : BaseApplication() {

    private fun getAppName(pID: Int): String? {
        var processName: String? = null
        val am = this
            .getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val l = am.runningAppProcesses
        val i = l.iterator()
        while (i.hasNext()) {
            val info = i
                .next() as ActivityManager.RunningAppProcessInfo
            try {
                if (info.pid == pID) {
                    processName = info.processName
                    return processName
                }
            } catch (e: Exception) {
                // Log.d("Process", "Error>> :"+ e.toString());
            }

        }
        return processName
    }


    override fun onCreate() {
        super.onCreate()
        Utils.initialize(this)
        LoadUtils.Builder()
            .addCallback(LoadingCallback::class)
            .addCallback(EmptyCallback::class)
            .addCallback(ErrorCallback::class)
            .addCallback(NetworkCallback::class)
            .setDefaultCallback(LoadingCallback::class)
            .commit()

//设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white)
            layout.setReboundDuration(1000)
            layout.setDisableContentWhenLoading(false)
            BezierRadarHeader(this@APP)
        }
        val pid = android.os.Process.myPid()
        val processAppName = getAppName(pid)
        if (processAppName == null || !processAppName.equals(
                applicationContext.packageName,
                true
            )
        ) {

            // 则此application::onCreate 是被service 调用的，直接返回
            return
        }

        MobSDK.init(this,"302deaf086d81","056222b2817640cf833d1917d178a118")
    }

}